<template>
  <div class="content" id="drawerContent">
    <a-row>
      <a-col :span="24" v-show="!showPermission">
        <!--查询参数-->
        <params
          ref="params"
          @handleRefresh="handleRefresh"
          :loading="loading"
        ></params>
        <!--表格页面-->
        <my-table
          ref="myTable"
          :dataSource="dataSource"
          :columns="columns"
          :loading="loading"
          @handleAdd="handleAdd"
          @removeSubmit="removeSubmit"
          @handleEdit="handleEdit"
          @handleImport="handleImport"
          @handleExport="handleExport"
          @handleDetail="handleDetail"
          @loadData="loadData"
          addAction="role_create"
          deleteAction="role_delete"
          editAction="role_edit"
          detailAction="role_details" 
        >
          <template v-slot:buttons="{ item }">
            <a @click="handlePermission(item)" v-action:role_audit
              ><a-icon type="snippets" />权限分配</a
            >
          </template>
          <template
            v-if="row.permissions"
            slot="expandedRowRender"
            slot-scope="row"
          >
            <a-row :gutter="10" :style="{ marginBottom: '12px' }">
              <a-col
                :span="12"
                :key="index"
                :style="{ marginBottom: '12px' }"
                v-for="(permission, index) in row.permissions"
              >
                <a-col :span="4">
                  <span>{{ permission.menuName }}：</span>
                </a-col>
                <a-col :span="20" v-if="permission.operations">
                  <a-tag
                    color="cyan"
                    v-for="(operation, key) in permission.operations"
                    :key="key"
                    >{{ operation }}</a-tag
                  >
                </a-col>
                <a-col :span="20" v-else>-</a-col>
              </a-col>
            </a-row>
          </template>
        </my-table>
        <!--修改页面-->
        <modify
          v-if="modifyEntity.showModify"
          @handleRefresh="handleRefresh"
          :modifyEntity="modifyEntity"
        />
      </a-col>
      <a-col :span="24" v-if="showPermission">
        <permission @handleHidePermission="handleHidePermission" :role="role" />
      </a-col>
    </a-row>
  </div>
</template>
<script>
import modify from "./modify";
import permission from "./permission";
import myTable from "_c/myTable/index";
import params from "./components/params";
import { roleColumns, queryRole, deleteRole } from "@/api/sys/permission/role";
export default {
  name: "Role",
  components: { params, modify, myTable, permission },
  data() {
    return {
      modifyEntity: {
        // 传递的主键
        id: "",
        // 页面标题
        title: "",
        // 页面操作类型
        operation: "",
        // 是否展示编辑页面
        showModify: false,
      },
      // 加载动画
      loading: false,
      // 绑定的数据源
      dataSource: [],
      // 表格绑定的列
      columns: roleColumns,
      // 是否显示权限分配页面
      showPermission: false,
      // 传递给权限分配页面的角色ID
      role: {},
    };
  },
  mounted() {
    this.loadData();
  },
  methods: {
    //#region 分配权限
    // 打开权限分配界面
    handlePermission(item) {
      this.role = item;
      this.showPermission = true;
    },
    // 隐藏权限分配页面
    handleHidePermission() {
      this.role = {};
      this.showPermission = false;
      this.handleRefresh();
    },
    //#endregion

    //#region 查询列表
    // 保存/修改后刷新表格
    handleRefresh() {
      this.modifyEntity.showModify = false;
      this.$refs.myTable.pagination.current = 1;
      this.loadData();
    },
    // 加载数据
    loadData() {
      this.loading = true;
      let queryParams = {
        ...this.$refs.params.params,
        current: this.$refs.myTable.pagination.current,
        pageSize: this.$refs.myTable.pagination.pageSize,
      };
      let that = this;
      queryRole(queryParams)
        .then((res) => {
          let { data, totalCount } = res.data;
          that.dataSource = data;
          that.$refs.myTable.pagination.total = totalCount;
          that.loading = false;
        })
        .catch(() => {
          that.loading = false;
        });
    },
    //#endregion

    //#region 添加
    // 打开添加页面
    handleAdd() {
      this.modifyEntity = {
        title: "新增角色信息",
        showModify: true,
        operation: this.operationFlag.Add,
      };
    },
    //#endregion

    //#region 删除
    // 删除
    removeSubmit(ids) {
      deleteRole(ids).then((res) => {
        if (res.code==200) {
          this.$notification.success({ description: res.msg });
          this.handleRefresh();
        } else {
          this.$notification.error({ description: res.msg });
        }
      });
    },
    //#endregion

    //#region 修改
    // 打开编辑页面
    handleEdit(item) {
      this.modifyEntity = {
        id: item.id,
        title: "修改角色信息",
        showModify: true,
        operation: this.operationFlag.Edit,
      };
    },
    //#endregion

    //#region 详情
    // 打开详情页面
    handleDetail(item) {
      this.modifyEntity = {
        id: item.id,
        title: "角色信息详情",
        showModify: true,
        operation: this.operationFlag.Detail,
      };
    },
    //#endregion

    //#region 导入
    // 导入数据
    handleImport() {},
    //#endregion

    //#region 导出
    // 导出数据
    handleExport() {},
    //#endregion
  },
};
</script>


